package com.ruoyi.develop.lvFukuan.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.service.IGoodsSalebuyService;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.service.IGoodsSalebuyallService;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.service.ILvBuyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvFukuan.domain.LvFukuan;
import com.ruoyi.develop.lvFukuan.service.ILvFukuanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 到货付款Controller
 * @author qh
 * @date 2024-09-14
 */
@RestController
@RequestMapping("/lvFukuan/lvFukuan")
public class LvFukuanController extends BaseController {
    @Autowired
    private ILvFukuanService lvFukuanService;

    @Autowired
    private ILvBuyService lvBuyService;

    @Autowired
    private IGoodsSalebuyallService goodsSalebuyallService;

    @Autowired
    private IGoodsSalebuyService goodsSalebuyService;
    /**
     * 查询到货付款列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFukuan:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvFukuan entity) {
        startPage();
        List<LvFukuan> list = lvFukuanService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出到货付款列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFukuan:export')")
    @Log(title = "到货付款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvFukuan entity) {
        List<LvFukuan> list = lvFukuanService.selectList(entity);
        ExcelUtil<LvFukuan> util = new ExcelUtil<LvFukuan>(LvFukuan. class);
        util.exportExcel(response, list, "到货付款数据");
    }

    /**
     * 获取到货付款详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFukuan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvFukuanService.selectById(id));
    }

    /**
     * 新增到货付款
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFukuan:add')")
    @Log(title = "到货付款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvFukuan entity) {
        return toAjax(lvFukuanService.insert(entity));
    }

    /**
     * 修改到货付款
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFukuan:edit')")
    @Log(title = "到货付款", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvFukuan entity) {
        return toAjax(lvFukuanService.update(entity));
    }

    /**
     * 删除到货付款
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFukuan:remove')")
    @Log(title = "到货付款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvFukuanService.deleteByIds(ids));
    }

    @PostMapping("/addFk")
    public AjaxResult addFk(@RequestBody LvFukuan entity) {
        return toAjax(lvFukuanService.insertFk(entity));
    }

    @DeleteMapping("/deleteById/{id}")
    public AjaxResult deleteById(@PathVariable String id) {
        LvFukuan lvFukuan=lvFukuanService.selectById(id);
        LvBuy lvBuy=lvBuyService.selectById(lvFukuan.getCaigouid());
        lvBuy.setFukuan(lvBuy.getFukuan().subtract(lvFukuan.getMoney()));
        lvBuy.setShengyu(lvBuy.getMoney().subtract(lvBuy.getFukuan()));
        lvBuyService.update(lvBuy);
        List<GoodsSalebuyall> goodsSalebuyalls=goodsSalebuyallService.selectMainId(lvFukuan.getCaigouid());
        for(GoodsSalebuyall goodsSalebuyall:goodsSalebuyalls){
            if(("," + lvFukuan.getId()).equals(goodsSalebuyall.getDakuanId())){
                goodsSalebuyall.setDakuanId(goodsSalebuyall.getDakuanId().replace("," + lvFukuan.getId(), ""));
            }else{
                goodsSalebuyall.setDakuanId(goodsSalebuyall.getDakuanId().replace(lvFukuan.getId(), ""));
            }
            if(("," + lvFukuan.getNos()).equals(goodsSalebuyall.getDakuanno())){
                goodsSalebuyall.setDakuanno(goodsSalebuyall.getDakuanno().replace("," + lvFukuan.getNos(), ""));
            }else{
                goodsSalebuyall.setDakuanno(goodsSalebuyall.getDakuanno().replace(lvFukuan.getNos(), ""));
            }
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            if(("," +  formatter.format(lvFukuan.getDdDatepeople())).equals(goodsSalebuyall.getDakuandate())){
                goodsSalebuyall.setDakuandate(goodsSalebuyall.getDakuandate().replace("," + formatter.format(lvFukuan.getDdDatepeople()), ""));
            } else{
                goodsSalebuyall.setDakuandate(goodsSalebuyall.getDakuandate().replace(formatter.format(lvFukuan.getDdDatepeople()), ""));
            }
            goodsSalebuyallService.update(goodsSalebuyall);
            List<GoodsSalebuy> goodsSalebuyList=goodsSalebuyService.selectByCgallId(goodsSalebuyall.getId());
            for(GoodsSalebuy goodsSalebuy:goodsSalebuyList){
                if(("," + lvFukuan.getId()).equals(goodsSalebuy.getDakuanId())){
                    goodsSalebuy.setDakuanId(goodsSalebuy.getDakuanId().replace("," + lvFukuan.getId(), ""));
                }else{
                    goodsSalebuy.setDakuanId(goodsSalebuy.getDakuanId().replace(lvFukuan.getId(), ""));
                }
                if(("," + lvFukuan.getNos()).equals(goodsSalebuy.getDakuanno())){
                    goodsSalebuy.setDakuanno(goodsSalebuy.getDakuanno().replace("," + lvFukuan.getNos(), ""));
                }else{
                    goodsSalebuy.setDakuanno(goodsSalebuy.getDakuanno().replace(lvFukuan.getNos(), ""));
                }
                if(("," +  formatter.format(lvFukuan.getDdDatepeople())).equals(goodsSalebuy.getDakuandate())){
                    goodsSalebuy.setDakuandate(goodsSalebuy.getDakuandate().replace("," + formatter.format(lvFukuan.getDdDatepeople()), ""));
                } else{
                    goodsSalebuy.setDakuandate(goodsSalebuy.getDakuandate().replace(formatter.format(lvFukuan.getDdDatepeople()), ""));
                }
                goodsSalebuyService.update(goodsSalebuy);
            }
        }
        return toAjax( lvFukuanService.deleteById(id));
    }
}
